<?php

/**
 * Database Class
 * @author ReGeDa
 */
class Database extends Plugin_Object {

	/**
	 * Connection handle
	 * @var PDO
	 */
	public $dataobject;

	/**
	 * Constructor
	 */
	public function __construct() {

		// configuration
		$config = array();

		// read config
		platcode()->config->import('database', $config);

		try {

			// create connection
			$this->dataobject = new PDO(
				sprintf('%s:host=%s;port=%d;dbname=%s', $config['driver'], $config['host'], $config['port'], $config['dbname']),
				$config['user'],
				$config['password'],
				array(
					PDO::ATTR_PERSISTENT => $config['is_persistant']
				)
			);

		} catch(PDOException $e) {
			throw new Platcode_Exception($e->getMessage());
		}

		// debugging flag
		if ($config['is_debug'])
			$this->dataobject->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	}

	/**
	 * Get persistant plugin state
	 * @override
	 * @return boolean
	 */
	public function is_persistant() {
		return TRUE;
	}
}

// end of file